<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column v-slot="{ $index }" label="序号" width="120" type="index">
                {{ pageConfig.pagesize * (pageConfig.page - 1) + $index + 1 }}
              </el-table-column>
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column v-slot="obj" label="操作">
                <!-- {{ obj.row.id }} -->
                <el-button size="small" type="success" @click="showPerm(obj.row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="editRold(obj.row)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRole(obj.row.id)">删除</el-button>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="total"
                :page-size="pageConfig.pagesize"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>

    <el-dialog :title="role.id?'编辑角色':'新增角色'" :visible.sync="isShowDialog">
      <el-row>
        <el-form ref="addForm" :model="role" :rules="rules" status-icon label-width="100px" class="demo-ruleForm">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="role.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="role.description" type="textarea" autocomplete="off" />

          </el-form-item></el-form>
      </el-row>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <el-dialog title="分配权限" :visible="isShowPermission" @close="btnCancelPerm">
      <!--tree 树形结构 -->
      <!-- show-checkbox 节点是否可被选择-->
      <!-- check-strictly 在显示复选框的情况下，是否严格的遵循父子不互相关联的做法 -->
      <el-tree
        ref="permTree"
        :data="permList"
        :props="{label:'name'}"
        default-expand-all
        show-checkbox

        check-strictly
        node-key="id"
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="btnCancelPerm">取消</el-button>
        <el-button size="small" type="primary" @click="btnOKPerm">确认</el-button>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import { delRole, getCompanyInfo, getRoleList, addRole, editRole, getRoleDetails } from '@/api/setting'
import { assignPerm, getPermList } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    // const checkRepeatName = async(rule, value, callback) => {

    // }
    return {
      isShowPermission: false,
      permList: [],
      roleId: '',

      companyInfo: {},
      roleList: [],
      pageConfig: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      isShowDialog: false,
      role: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
          // { validator: checkRepeatName, trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.getCompany()
    this.getRoleList()
    this.getPermList()
  },
  methods: {
    async btnOKPerm() {
      console.log(this.$refs.permTree.getCheckedKeys())
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      })
      this.$message.success('修改成功')
      this.isShowPermission = false
    },

    btnCancelPerm() {
      this.$refs.permTree.setCheckedKeys([])
      this.isShowPermission = false
    },
    // 显示分配权限弹框
    async showPerm(id) {
      this.roleId = id
      this.isShowPermission = true
      const { permIds } = await getRoleDetails(id)
      // console.log(res)
      this.$refs.permTree.setCheckedKeys(permIds)
    },
    // 获取权限列表
    async getPermList() {
      this.permList = listToTreeData(await getPermList(), '0')
    },
    async getCompany() {
      this.companyInfo = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    // 角色列表
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageConfig)
      // console.log(rows)
      this.roleList = rows
      this.total = total
    },
    // 删除
    async delRole(id) {
      await this.$confirm('是否确认删除')
      await delRole(id)
      this.$message.success('删除成功')
      if (this.roleList.length === 1 && this.pageConfig.page > 1) {
        this.pageConfig.page--
      }
      this.getRoleList()
    },
    // 编辑
    async editRold(obj) {
      console.log(obj)
      this.role = obj
      this.isShowDialog = true

      const res = await editRole(this.role)
      console.log(res)
    },

    // 新增
    addRole() {
      this.isShowDialog = true
    },

    async btnOK() {
      // 对整个表单进行验证
      await this.$refs.addForm.validate()

      if (this.role.id) {
        const res = await editRole(this.role)// 编辑
        console.log(res)
      } else {
        const res = await addRole(this.role)// 新增
        console.log(res)
      }

      this.getRoleList()// 更新数据

      this.$message.success('新增成功')
      // 清空数据
      this.$refs.addForm.resetFields()
      // 关闭弹框
      this.isShowDialog = false
    },
    btnCancel() {
      // 清空数据
      this.$refs.addForm.resetFields()
      // 关闭弹框
      this.isShowDialog = false

      this.role = {
        name: '',
        description: ''
      }
    },

    // 当当前页改变时触发事件
    currentChange(val) {
      // console.log(val)
      // console.log(this.pageConfig)
      this.pageConfig.page = val
      this.getRoleList()
    }
  }
}
</script>

<style>

</style>
